<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\restaurant\server;

use app\restaurant\model\OrderAddress;

use app\restaurant\model\Progress;
use app\restaurant\model\Store;

use app\restaurant\model\Order;

use app\restaurant\model\TransporterOrder;


class StoreApi{


    protected $order_model;

    protected $Transporter;

    protected $appkey;

    protected $appsec;

    protected $source_id;

    protected $shop_no;

    protected $dadaApi;

    public function __construct()
    {

        $this->order_model = new Order();

        $this->Transporter = new TransporterOrder();


    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-29 14:18
     * @功能说明:获取订单配送费
     */
    public function getDeliverFee($id){

        $order = $this->findOrder($id);

        if(!empty($order['code'])){

            return $order;
        }

        $cash = 0.01;

        $this->order_model->orderUpdate(['order_code'=>$id],['express_price'=>$cash,'pay_price'=>$order['pay_price']+$cash]);

        $order = $this->order_model->orderInfo(['order_code'=>$id]);

        return $order;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-30 18:49
     * @功能说明:查找订单
     */
    public function findOrder($order_id){

        $store_model   = new Store();

        $address_model = new OrderAddress();
        //订单信息
        $order = $this->order_model->orderInfo(['order_code'=>$order_id]);

        if(empty($order)){

            return ['code'=>500,'msg'=>'订单不存在'];
        }

        $order['city'] = $store_model->where(['id'=>$order['store_id']])->value('city');

        if(empty($order['city'])){

            return ['code'=>500,'msg'=>'该门店未选城市'];

        }

        $order['address'] = $address_model->dataInfo(['order_id'=>$order['id']]);

        if(empty($order['address'])){

            return ['code'=>500,'msg'=>'地址信息错误'];

        }

        return $order;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-30 19:21
     * @功能说明:查询订单后发单(备用)
     */
    public function addAfterQuery($order_id){

        $order = $this->findOrder($order_id);

        if(!empty($order['code'])){

            return $order;
        }

        $data['deliveryNo'] = $order['delivery_no'];
        //发单
        $result = $this->dadaApi->addAfterQuery($data);
        //配送信息表
        $transporter_info = $this->Transporter->orderInfo(['order_id'=>$order['id']]);

        if(empty($transporter_info)){

            $insert = [

                'uniacid'    => $order['uniacid'],

                'order_id'   => $order['id'],

                'order_code' => $order['order_code'],

                'status'     => 1

            ];

            $this->Transporter->addOrder($insert);
        }

        return $result;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-30 10:52
     * @功能说明:商家发单
     */
    public function addOrder($order_id){

        $order = $this->findOrder($order_id);

        if(!empty($order['code'])){

            return $order;
        }
        //配送信息表
        $res = $this->Transporter->where(['order_code'=>$order_id])->update(['status'=>1]);

        if($res!=1){

            return ['code'=>500,'msg'=>'发单失败'];
        }
        //加入配送进度
        $progress_model = new Progress();

        $progress_model->storeAcceptOrder($order['uniacid'],$order['order_code']);

        return $res;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-30 19:18
     * @功能说明:商家重新发单(如果三方平台存在异常是需要重新发单的)
     */
    public function reAddOrder($order_id){

        $order = $this->findOrder($order_id);

        if(!empty($order['code'])){

            return $order;
        }

        $result = $this->dadaApi->addOrder($order);

        return $result;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-30 18:52
     * @功能说明:订单详情
     */
    public function orderInfo($order_id){

        $order = $this->findOrder($order_id);

        if(!empty($order['code'])){

            return $order;
        }
        //达达查询订单信息
        $result = $this->dadaApi->orderInfo($order);

        if(isset($result['code'])&&$result['code']==0){

            return $result['result'];

        }else{

            return ['code'=> 500,',msg'=>$result['msg']];
        }


    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-30 18:52
     * @功能说明:订单取消
     */
    public function orderCancel($order_id,$cancel_reason_id=4,$cancel_reason=''){

        $order = $this->findOrder($order_id);

        if(!empty($order['code'])){

            return $order;
        }

        $arr['order_id'] = $order['order_code'];

        $arr['cancel_reason_id'] = $cancel_reason_id;

        if(!empty($cancel_reason)){

            $arr['cancel_reason'] = $cancel_reason;
        }

        //达达取消订单(测试)
       // $result = $this->dadaApi->cancelOrder($arr);
        //达达取消订单(正式)
        $result = $this->dadaApi->formalCancel($arr);

        return $result;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-12 17:40
     * @功能说明:
     */
    public function check($store_id = 0){

        return 1;

    }



}